<?php

class Application_Model_DbTable_Booking extends Zend_Db_Table_Abstract
{

    protected $_name = 'booking';

    public function insertBooking($data){
        // Insert into booking table
        $newData = array(
            'code' => $data['code'],
            'user_id' => $data['user_id'],
            'venue_id' => $data['venue_id'],
            'booking_time' => $data['booking_time'],
            'start_date' => $data['start_date'],
            'duration' => $data['duration'],
            'total_payment' => $data['total_payment'],
            'organizer' => $data['organizer'],
            'participant' => $data['participant']
        );
        $this->insert($newData);
        
        // Insert booking date
        $bid = $this->getBooking('code',$data['code'])->booking_id;
        $db = $this->getAdapter();
        $idate = strtotime($data['start_date']); //start_date in integer
        for($i=0; $i<$data['duration']; $i++){
            $date = date("Y/m/d",$idate + ($i*24*60*60));
            $db->insert('booking_date', array('booking_id'=>$bid,'date'=>$date));
        }
    }

    public function getBooking($key,$value){
        if(is_string($value)) $value = "'$value'";

        $query = $this->select();
        $query->from(array('b'=>'booking'));
        $query->joinInner(array('bs'=>'booking_status'), 'b.booking_status_id = bs.booking_status_id', array('bstatus_name'=>'bs.name'));
        $query->joinInner(array('ps'=>'payment_status'), 'b.payment_status_id = ps.payment_status_id', array('pstatus_name'=>'ps.name'));
        $query->joinInner(array('v'=>'venue'), 'b.venue_id = v.venue_id', array('v_name'=>'v.name'));
        $query->where("b.$key = $value");
        $query->setIntegrityCheck(false);

        return $this->fetchRow($query);
    }

    public function getAllBookings($key,$value){
        if(is_string($value)) $value = "'$value'";

        $query = $this->select();
        $query->from(array('b'=>'booking'));
        $query->joinInner(array('bs'=>'booking_status'), 'b.booking_status_id = bs.booking_status_id', array('bstatus_name'=>'bs.name'));
        $query->joinInner(array('ps'=>'payment_status'), 'b.payment_status_id = ps.payment_status_id', array('pstatus_name'=>'ps.name'));
        $query->joinInner(array('v'=>'venue'), 'b.venue_id = v.venue_id', array('v_name'=>'v.name'));
        $query->where("b.$key = $value");
        $query->order('b.booking_time desc');
        $query->setIntegrityCheck(false);

        return $this->fetchAll($query);
    }

    public function cancelbooking($id, $reason){
        $statusId = $this->getBooking('booking_id',(int)$id)->booking_status_id;
        switch ($statusId) {
            case 1: $statusId = -1; break;
            case 2: $statusId = -2; break;
        }
        $db = $this->getAdapter();

        //update status
        $this->update(array('booking_status_id'=>$statusId), "booking_id = $id");

        //insert reason
        $db->insert('booking_cancel', array('booking_id'=>$id,'reason'=>$reason));

        //delete booking date
        $db->delete('booking_date', "booking_id = $id");
    }

    public function getCancelReason($id){
        $db = $this->getAdapter();
        $row = $db->fetchRow("SELECT reason FROM booking_cancel WHERE booking_id = $id");
        return $row['reason'];
    }
}

